/**
 * 可移动的地图
 */
class MobileMap extends egret.DisplayObjectContainer {
	public scaleX = 1;
	public scaleY = 1;
	private orginCoord: TiledMap.Point = new TiledMap.Point(0, 0);
	private map: TileMap;
	public mapLayer: egret.DisplayObjectContainer;
	private urlloader: egret.URLLoader;

	private imageloader = {
		showAsyncByCrossUrl(image: egret.DisplayObjectContainer, url: string, x?: number, y?: number, w?: number, h?: number) {
			var texture = RES.getRes(url.replace("/", "_").replace(".png", ""));
			var bitmap = new egret.Bitmap(texture);
			bitmap.x = x || 0;
			bitmap.y = y || 0;
			bitmap.width = w || texture.textureWidth;
			bitmap.height = h || texture.textureWidth;
			image.addChild(bitmap);
		}
		
	};
	public createTileMap(initX: number, initY: number) {
		this.orginCoord.x = initX;
		this.orginCoord.y = initY;
		this.mapLayer = new egret.DisplayObjectContainer();
		this.mapLayer.width = Math.floor(this.stage.width / this.scaleX);
		this.mapLayer.height = Math.floor(this.stage.width / this.scaleY);
		this.mapLayer.x = - this.mapLayer.width/2;
		this.mapLayer.y = - this.mapLayer.height/2;
		this.addChild(this.mapLayer);
		TileMap.createTileMap("0.tmx", "resource/mir2/map/", function (map: TileMap, progress: number, total: number) {
			console.log('[INFO] load tilemap [' + map.name + '] progress: ' + progress + "/" + total);
			if (progress == total) {
				this.map = map;
				TileMap.render(map, this.mapLayer, this.orginCoord.x, this.orginCoord.y, this.mapLayer.width, this.mapLayer.height);
			}
		}.bind(this));
	}


	public randomMove() {
		var x = Math.floor(Math.random() * this.map.width);
		var y = Math.floor(Math.random() * this.map.height);
		TileMap.render(this.map, this.mapLayer, x, y, this.mapLayer.width, this.mapLayer.height);
		return { x: x, y: y };
	}
	public goSafeArea() {
		TileMap.render(this.map, this.mapLayer, 333, 333, this.mapLayer.width, this.mapLayer.height);
		return { x: 333, y: 333 };
	}
	public constructor() {
		super();
	}

	public move(direction: Direction, speed?: number) {
		var dstX = this.orginCoord.x, dstY = this.orginCoord.y;
		var tileNum = speed || 1;
		switch (direction) {
			case Direction.Up:
				dstY -= tileNum;
				break;
			case Direction.Down:
				dstY += tileNum;
				break;
			case Direction.Left:
				dstX -= tileNum;
				break;
			case Direction.Right:
				dstX += tileNum;
				break;
			case Direction.UpRight:
				dstY -= tileNum;
				dstX += tileNum;
				break;
			case Direction.RightDown:
				dstY += tileNum;
				dstX += tileNum;
				break;
			case Direction.DownLeft:
				dstX -= tileNum;
				dstY += tileNum;
				break;
			case Direction.LeftUP:
				dstX -= tileNum;
				dstY -= tileNum;
				break;
		}
		this.orginCoord.update(dstX,dstY); 
		TileMap.render(this.map, this.mapLayer, this.orginCoord.x, this.orginCoord.y, this.mapLayer.width, this.mapLayer.height);
		// egret.Tween.removeTweens(this.render);
		// egret.Tween.get(this.render, { loop: false }).to({ x: this.x * this.tileWidth * -1, y: this.y * this.tileHeight * -1 }, 750)
		// 	.call(function () {
		// 		this.render.x;
		// 		console.log('[INFO] render :' + this.render.x + "," + this.render.y);
		// 	}.bind(this));

	}
}